package com.lenovo.smarttraffic.ui.fragment.stage3;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.SensorDataDB;
import com.lenovo.smarttraffic.ui.fragment.MyBaseFragment;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class LifeHelperFragment extends MyBaseFragment {

    private int type;
    private BarChart mBcWork7;
    private LineChart mLcWork7;
    private View view;
    private List<BarEntry> barEntries;
    private BarDataSet barDataSet;
    private ArrayList<Date> dates = new ArrayList<>();
    private TextView mTvTableDesc;
    private LineDataSet lineDataSet;
    private ArrayList<Entry> entries;

    public LifeHelperFragment setType(int type) {
        this.type = type;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_life_helper, container, false);
        mBcWork7 = view.findViewById(R.id.bc_work7);
        mLcWork7 = view.findViewById(R.id.lc_work7);
        mTvTableDesc = view.findViewById(R.id.tv_table_desc);

        initTable();
        initData();
        return view;
    }

    private void initTable() {
        switch (type) {
            case 0:
                showBarChart();
                break;
            case 1:
                showLineChart(1);
                break;
            case 2:
                showLineChart(2);
                break;
            case 3:
                showLineChart(3);
                break;
        }
    }

    private void showLineChart(int aflag) {
        mBcWork7.setVisibility(View.GONE);
        mLcWork7.setVisibility(View.VISIBLE);
        //进行柱状图显示
        entries = new ArrayList<>();
        entries.add(new Entry(0, 0));
        lineDataSet = new LineDataSet(entries, "");
        List<ILineDataSet> lineDataSets = new ArrayList<>();
        lineDataSets.add(lineDataSet);
        LineData lineData = new LineData(lineDataSets);

        XAxis xAxis = mLcWork7.getXAxis();
        YAxis axisLeft = mLcWork7.getAxisLeft();

        //为页面做单独设置
        if (aflag == 1) {
            lineDataSet.setColor(Color.GRAY);
            lineDataSet.setCircleColor(Color.GRAY);
            lineDataSet.setDrawValues(false);
        }
        if (aflag == 2) {
            lineDataSet.setColor(Color.GRAY);
            lineDataSet.setCircleColor(Color.GRAY);
            lineDataSet.setDrawValues(false);
        }
        if (aflag == 3) {
            lineDataSet.setColor(Color.WHITE);
            lineDataSet.setCircleColor(Color.WHITE);
            mLcWork7.setBackground(getActivity().getDrawable(R.color.Blue));
            lineDataSet.setDrawValues(false);
            xAxis.setGridColor(Color.WHITE);
            xAxis.setTextColor(Color.WHITE);
            xAxis.setAxisLineColor(Color.WHITE);
            axisLeft.setZeroLineColor(Color.WHITE);
            axisLeft.setGridColor(Color.WHITE);
            axisLeft.setTextColor(Color.WHITE);
            axisLeft.setAxisLineColor(Color.WHITE);
        }

        xAxis.setValueFormatter((value, axis) -> {
            if (dates.size() != 0) {
                Date date = dates.get((int) value);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
                return simpleDateFormat.format(date);
            } else {
                return "时间信息错误";
            }
        });


        Description description = new Description();
        description.setEnabled(false);
        mLcWork7.setDescription(description);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        mLcWork7.getAxisRight().setEnabled(false);
        mLcWork7.setData(lineData);
        mLcWork7.invalidate();
    }

    private void showBarChart() {
        mBcWork7.setVisibility(View.VISIBLE);
        mLcWork7.setVisibility(View.GONE);

        barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(0, 0));

        barDataSet = new BarDataSet(barEntries, "空气质量");
        barDataSet.setColor(Color.GRAY);

        ArrayList<IBarDataSet> iBarDataSets = new ArrayList<>();
        iBarDataSets.add(barDataSet);
        BarData barData = new BarData(iBarDataSets);

        mBcWork7.getAxisRight().setEnabled(false);
        XAxis xAxis = mBcWork7.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        Description description = new Description();
        description.setText("");

        xAxis.setLabelRotationAngle(+45);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (dates.size() != 0) {
                    Date date = dates.get((int) value);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
                    return simpleDateFormat.format(date);
                } else {
                    return "时间信息错误";
                }
            }
        });

        mBcWork7.setDescription(description);
        mBcWork7.setData(barData);
        mBcWork7.invalidate();
    }

    private void initData() {
        CountDownTimer countDownTimer = new CountDownTimer(Long.MAX_VALUE, 3000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //读取数据库数据
                List<SensorDataDB> wcBeans = LitePal.findAll(SensorDataDB.class);
                //进行制表刷新
                switch (type) {
                    case 0:
                        barEntries.clear();
                        dates.clear();
                        float max = 0;
                        for (int i = 0; i < wcBeans.size(); i++) {
                            Float aFloat = Float.valueOf(wcBeans.get(i).getPm25());
                            barEntries.add(new BarEntry(i, aFloat));
                            //存入时间轴信息
                            dates.add(wcBeans.get(i).getDate());
                            //计算最大值
                            if (max < aFloat) {
                                max = aFloat;
                            }

                        }
                        //修改柱状图描述信息
                        mTvTableDesc.setText("过去一分钟空气质量最差值" + max);
                        barDataSet.notifyDataSetChanged();
                        XAxis xAxis = mBcWork7.getXAxis();
                        xAxis.setLabelCount(wcBeans.size());
                        mBcWork7.getBarData().notifyDataChanged();
                        mBcWork7.notifyDataSetChanged();
                        mBcWork7.invalidate();
                        break;
                    case 1:
                        //-------------更新折线表数据
                        float max1 = 0;
                        float min1 = 0;
                        entries.clear();
                        dates.clear();
                        for (int i = 0; i < wcBeans.size(); i++) {
                            Float aFloat = Float.valueOf(wcBeans.get(i).getTemperature());
                            entries.add(new Entry(i, aFloat));
                            //存入时间轴信息
                            dates.add(wcBeans.get(i).getDate());
                            //计算最大最小值
                            if (max1 < aFloat) {
                                max1 = aFloat;
                            }
                            if (min1 > aFloat) {
                                min1 = aFloat;
                            }
                        }

                        mTvTableDesc.setText(String.format(Locale.CHINA, "过去一分钟最高气温%d℃,最低%d℃", (int) max1, (int) min1));
                        lineDataSet.notifyDataSetChanged();
                        XAxis xAxis1 = mLcWork7.getXAxis();
                        xAxis1.setLabelCount(wcBeans.size());
                        mLcWork7.notifyDataSetChanged();
                        mLcWork7.getLineData().notifyDataChanged();
                        mLcWork7.invalidate();
                        break;
                    case 2:
                        //-------------更新折线表数据
                        float max2 = 0;
                        entries.clear();
                        dates.clear();
                        for (int i = 0; i < wcBeans.size(); i++) {
                            Float aFloat = Float.valueOf(wcBeans.get(i).getHumidity());
                            Log.i("123456", "onTick: "+wcBeans.get(i).getHumidity());
                            entries.add(new Entry(i, aFloat));
                            //存入时间轴信息
                            dates.add(wcBeans.get(i).getDate());
                            //计算最大最小值
                            if (max2 < aFloat) {
                                max2 = aFloat;
                            }
                        }
                        mTvTableDesc.setText(String.format(Locale.CHINA, "过去一分钟最高湿度%d%%", (int) max2));
                        XAxis xAxis2 = mLcWork7.getXAxis();
                        xAxis2.setLabelCount(wcBeans.size());
                        lineDataSet.notifyDataSetChanged();
                        mLcWork7.notifyDataSetChanged();
                        mLcWork7.getLineData().notifyDataChanged();
                        mLcWork7.invalidate();
                        break;
                    case 3:
                        //-------------更新折线表数据
                        float max3 = 0;
                        entries.clear();
                        dates.clear();
                        for (int i = 0; i < wcBeans.size(); i++) {
                            Float aFloat = Float.valueOf(wcBeans.get(i).getCo2());
                            entries.add(new Entry(i, aFloat));
                            //存入时间轴信息
                            dates.add(wcBeans.get(i).getDate());
                            //计算最大最小值
                            if (max3 < aFloat) {
                                max3 = aFloat;
                            }
                        }
                        mTvTableDesc.setText(String.format(Locale.CHINA, "过去一分钟二氧化碳最大浓度%d", (int) max3));
                        XAxis xAxis3 = mLcWork7.getXAxis();
                        xAxis3.setLabelCount(wcBeans.size());
                        lineDataSet.notifyDataSetChanged();
                        mLcWork7.notifyDataSetChanged();
                        mLcWork7.getLineData().notifyDataChanged();
                        mLcWork7.invalidate();
                        break;
                }
            }

            @Override
            public void onFinish() {

            }
        };
        countDownTimer.start();
    }


}
